﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToWeb.CApExportSimulationManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections.Generic;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToWeb;

internal class CApExportSimulationManager
{
  private CApC3DSimulationApp m_simulationApp = new CApC3DSimulationApp();
  private CApC3DSnapshotApp m_snapshotApp = new CApC3DSnapshotApp();
  private CApC3DRootNodeData m_rootOperation;

  internal CApExportSimulationManager(
    CApC3DRootNodeData rootOperation,
    TxExportGraphicsAnimationData exportData,
    TxExportGraphicsAnimationFileFormatEx format,
    bool export)
  {
    this.m_rootOperation = rootOperation;
    this.m_simulationApp.ExportData = exportData;
    this.m_simulationApp.Format = format;
    this.m_simulationApp.ExportSimulation = export;
  }

  internal CApC3DSnapshotApp SnapshotApp
  {
    get => this.m_snapshotApp;
    set => this.m_snapshotApp = value;
  }

  internal void Initialize()
  {
    this.m_simulationApp.Initialize((CApC3DProcessNodeData) this.m_rootOperation);
  }

  internal void InitializeWithGraphicExport(
    string fileName,
    string simulationFileName,
    CApC3DDocumentationSettings documentationSettings,
    CApC3DAppearanceSet appearanceSet)
  {
    this.m_simulationApp.Initialize((CApC3DProcessNodeData) this.m_rootOperation, fileName, simulationFileName, documentationSettings, appearanceSet);
  }

  internal bool ExportSimulation()
  {
    this.m_simulationApp.StartStepRecording += new CApC3DSimulationApp.CApC3DSimulationApp_StartStepRecordingEventHandler(this.m_simulationApp_StartStepRecording);
    this.m_rootOperation.IsStep = true;
    bool flag = this.RecordObject((CApC3DProcessNodeData) this.m_rootOperation);
    TxExportGraphicsAnimationEx.GetAnimationOutputData();
    this.m_simulationApp.StartStepRecording -= new CApC3DSimulationApp.CApC3DSimulationApp_StartStepRecordingEventHandler(this.m_simulationApp_StartStepRecording);
    return flag;
  }

  internal void Terminate() => this.m_simulationApp.Terminate();

  private void m_simulationApp_StartStepRecording(
    object sender,
    CApC3DSimulationApp.CApC3DSimulationApp_StartStepRecordingEventArgs args)
  {
    if (this.m_snapshotApp == null)
      return;
    this.m_snapshotApp.SetSnapshot(args.Step);
  }

  private bool RecordObject(CApC3DProcessNodeData operation)
  {
    bool flag = true;
    if (operation.IsStep)
    {
      this.ApplyFullSnapshot(operation.AttachedSnapshotData.Snapshot);
      flag = this.m_simulationApp.Record(operation);
    }
    if (flag)
    {
      List<CApC3DProcessNodeData> children = operation.GetChildren(true);
      int count = children.Count;
      for (int index = 0; index < count & flag; ++index)
        flag = this.RecordObject(children[index]);
    }
    return flag;
  }

  private void ApplyFullSnapshot(TxSnapshot snapshot)
  {
    if (!TxSnapshot.op_Inequality(snapshot, (ITxObject) null))
      return;
    snapshot.Apply(new TxApplySnapshotParams()
    {
      ObjectsLocation = true,
      ObjectsVisibility = true,
      PointOfView = true,
      DevicePoses = true,
      ObjectsAttachments = true,
      ObjectsColor = true,
      ObjectsViewingMode = true,
      PMIsScale = true
    });
  }
}
